﻿using System.Collections.Generic;
using MyFramework.MVCLibrary.View;
using MyFramework.Singleton;
using UnityEngine;

namespace MyFramework.MVCLibrary
{
    public class ViewManager : Singleton<ViewManager>
    {
        private readonly Dictionary<ViewType, BaseView> _viewDictionary = new Dictionary<ViewType, BaseView>();

        public ViewManager()
        {
            _viewDictionary.Add(ViewType.MainView,
                new MainView("MainView", ViewType.MainView, ScenesType.Start));
        }

        public BaseView GetView(ViewType type)
        {
            return _viewDictionary.GetValueOrDefault(type);
        }

        public BaseView OpenView(ViewType viewType)
        {
            if (_viewDictionary.TryGetValue(viewType, out BaseView view))
            {
                view.Open();
                return view;
            }

            Debug.LogError($"无法打开{viewType}窗口");
            return null;
        }

        public void CloseView(ViewType viewType)
        {
            if (_viewDictionary.TryGetValue(viewType, out BaseView view))
            {
                view.Close();
            }
            else
            {
                Debug.LogError($"无法关闭{viewType}窗口");
            }
        }

        public void PreLoadViews(ScenesType scenesType)
        {
            foreach (var item in _viewDictionary.Values)
            {
                if (item.GetScenesType() == scenesType)
                {
                    item.PreLoad();
                }
            }
        }
    }
}